package controller;


import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;



import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import dao.ThongsoDAO;

import pojo.Comment;
import pojo.Thongso;
import pojo.User;

@Controller

public class ThongSoController {
	
	@RequestMapping (value = "LietKeThongSo.do", method = RequestMethod.GET)
	public String lietkeQuangcao(
			HttpServletRequest request)
	{
    	HttpSession session = request.getSession();
    	User us = (User) session
				.getAttribute("user");
    	if(us==null)       return ("page/FrontEnd/DangNhap");
    	if(us.getIsAdmin()==0) return ("page/FrontEnd/DangNhap");
		int kq_soluongcm = 0;
		int kq_trang = 0;
		ThongsoDAO DAO= new ThongsoDAO();
		List<Thongso> lqc= DAO.getList();
		
		Thongso ts=lqc.get(0);
				
		

		request.setAttribute("ts", ts);		


		request.setAttribute("title", "Liệt kê thông số");

		return "page/BackEnd/LietKeThongSo";
	}	
	
	@RequestMapping(value ="SuaThongSo.do", method = RequestMethod.GET)
	public String suaQC(Model model,
			HttpServletRequest request
			)
	{
    	HttpSession session = request.getSession();
    	User us = (User) session
				.getAttribute("user");
    	if(us==null)       return ("page/FrontEnd/DangNhap");
    	if(us.getIsAdmin()==0) return ("page/FrontEnd/DangNhap");
		ThongsoDAO DAO= new ThongsoDAO();
		List<Thongso> lqc= DAO.getList();
		Thongso ts=lqc.get(0);	
		model.addAttribute("ts", ts);
		String link = "SuaThongSo.do";			
		model.addAttribute("link", link);
		return "page/BackEnd/SuaThongSo";
	}
	
	@RequestMapping(value ="SuaThongSo.do", method = RequestMethod.POST)
	public String suaQCPost(
			@RequestParam(value="soBookTrenTrang", required = false)String soBookTrenTrang,
			@RequestParam(value="soBookMacDinh", required = false)String soBookMacDinh,
			@RequestParam(value = "kieuSapXep", required = false) String kieuSapxep,
			HttpServletRequest request,
			Model model)
	{
    	HttpSession session = request.getSession();
    	User us = (User) session
				.getAttribute("user");
    	if(us==null)       return ("page/FrontEnd/DangNhap");
    	if(us.getIsAdmin()==0) return ("page/FrontEnd/DangNhap");
		String title="";
		String output="";
		ThongsoDAO dao = new ThongsoDAO();
		boolean flag=dao.capnhatThongSo(soBookTrenTrang, soBookMacDinh, kieuSapxep);
		if(flag==true)
		{

			return  "redirect:LietKeThongSo.do";
		} 
		else
		{
			String link="LietKeThongSo.do";
			model.addAttribute("title",title);
			model.addAttribute("output", output);
			model.addAttribute("link", link);
			return  "template/ketqua";
		}
		
	
	}
}


